package com.song.mall.order.config;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.song.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000118632701";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String  merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCIDaQXhwtPzTLh635m2QIvIpJEh9uw4X8QEXpsAtuurZ5URzSutjYUQ9QesTwI9XGg9lr89ptMqoESpUxqszgod/cUnuPK7LRHvNzHJqGmKIgSsOnHwuT/gRwo+a2qM7RSxYtjUSktDHgkjqZwM7jL10dJ86r2kkYZ07vN3RGna9eB/xyiLmoVuVEWESy2G8EqWwMIqd8lpzt/T/VyEoTcStad5lohfNvKpZj1WpyUuZXedFgmGIMRB81YsjWyRm0ymF4qx/1vehLHR60F/vAZ6ZAfB5ssrqUEB6fRdW9K7o/7nNlMbFSQZkADQpO6lxPJkiovXko9IjAH0RLT9cTNAgMBAAECggEAIyxbotV/4Cato+iM6ivqO6NVazAwZL83aUELhEcM4lDyYNlzzkM/IegTO8gYPDywHqvyA/oBVkWyTs67Pc6NkzDIiOU3AOxHA1DZRwc+vUU3QJXKKqHBMCkBzboW4qyxTsDRc2C1I3dYzCKw9sr4WIKUZJMBPPO9+clagTNI8B45vNveMkm6j2+PQpXOM5KKXr4KOtSAlzhXcLMTYkx5qc6pMStbfJaaz8zTiiZTViukp7uwfYGBlgD+gR3WAznY6gSvmdgwT1aQ6ZoywmK336FwTqB4ZQ/IU0FUGNSZIMhu3EP+DUFpFEpoFXaKX4ENxaHuryLDce867jT8EgW+AQKBgQC81PA3Ea8yPH1ZHVsDpVerfWnu2g1cOoo3rl5qUbiIbEw2WW4IfrTYLHzuz1JzV01OpKPpGfVuqeUeyWl6dABRAxDDH5FTGWUoqChQOPKh9rxvWnYOi0oOegXo+ODYFtamwPs9wQppsIy8mv8UaEIYhxxsm5iwi9fVATfSAt89TQKBgQC4cq5chOD+cfWIZqHPqC6XoMHcs7cBz+MXHEGq9NacFHyYBS7kWYtUEdewEmQ+/Xb+VP2u7WeGNwSi9DXXtWYvd6dAKtdDAne6DkYVKHflXOuGB9bfD0vwSfKlUt24nrKMAhBHn2VESCDDlEiEJA9tqIOLuuIxn3s04gE3pSrlgQKBgBmWALZ1fvefmpxojdyAmtBxvdjPUuAcTn4c8k4VpJtXkTwMAgzEnlxsKsFw9gsK2T6+sDb1fXvxKUpyoAgnj6Nqdur5bAeGeV546n10OewYMpAkAbsWxIIRpptanZyp+4u8uk4zyeVoNqmXlth9B/eq1v8gViAV0rjZ4WZjXTxBAoGAW13zoUDxcxlgV0D/xq245sNrRLiBXxRrmXcV0GmYJZjAX02x/MueiPaNsVpq7gkAeNwBDjXrokB+u3ndcg0ZoUnUP6T5HOiMBk4ryS9StPtib/JSLgyVwWHgIKT70AhMya38An6i20AfesJpaYuzWM6w2vaoNYyaq4eIstfOeAECgYAA/XFW6UWoFc16ALT9vVMru2XxwkxUanXQVzO1Cky3AZW6bf+IrH3qcJRvL2HAKL9K6LTKKELVsb3YN/cb8uDwXtWfGK3YNXi/fxUsagZg/Dps77CFCQKM4QoxJj6nQZ7TRt0xfhZ7e7ReoG+RpIHwyH/Opf2AUPvGNduskcKweg==";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAh1FlfWlOZLgRU6qKxZhZNF8aQLWkrDp1VpSDfO9lBvM4Z8+EPfIF4OicimNAanjsPRNCTabo+L3KMI3Y8Tw8FZjKxTzNQnYyTBLfG4+VuOAIHL+jPR3doT8yYiRecywDLGq/Fydn+notQLKzwbTHeFxeezVae/j/x48AGkGFqi+1GHH0ZWRPgIhWf4cpxQWYQllY9HEFBg2uM1dKt7mK8nMiWe9MHrIShKvEuoygo1Ln9ExalPTAXksCuAVtvN2iS9HJK0TQnNolxcMAUYnbhFDZP31bG5e+u3jh2X/DWPvCyQh/DdPqFqfM0Qi02t1ijr/PZd/N36p06ZPMNXefWwIDAQAB";
    // 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //只要支付支付成功了，会每隔几秒发送一条消息，告知支付成功了，可以接收到这个消息以后，后台修改一下订单的这个成功页
    public static String notify_url = "http://c9fm2p.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //支付宝支付成功以后要跳转的页面地址
    public static String return_url = "http://member.mall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
